using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Web.AppCode;
using Action = Edge.Common.Web.Navigation.Action;

namespace Shop.Web
{
    public partial class BrandDetails : ShopBasePage
    {

        [QueryParameter("action", IsRequired = false)]
        public Action action = Action.New;

        [QueryParameter("id", IsRequired = false)]
        public int id;
        
        protected void Page_Load(object sender, EventArgs e)
        {   
            WebParameterAttribute.SetValues(this, Request);
            HandlePage();
            imgLogo.ImageUrl = ShopBasePage.NoImage;
            if (!IsPostBack)
            {
                if (id > 0 && action == Action.New)
                    action = Action.View;
                switch (action)
               {
                   case Action.View:
                       tbName.IsLabel = true;
                       tbDescription.IsLabel = true;
                       btnEdit.Visible = true;
                       btnDelete.Visible = true;
                       fuImage.Visible = false;
                       break;
                   case Action.New:
                       tbName.IsLabel = false;
                       tbDescription.IsLabel = false;
                       btnSave.Visible = true;
                       btnCancel.Visible = true;
                       break;
                   case Action.Edit:
                       btnSave.Visible = true;
                       btnCancel.Visible = true;
                       btnDelete.Visible = true;
                       break;
                   default:
                       break;

               }
               DataBind();
            }
        }

        protected void DataBind()
        {
            Brand brand = null;
            if (id > 0)
                brand = Locator.GetService<IBrandManager>().Load(id);
            if (brand != null)
            {
                tbName.Text = brand.Name;
                tbDescription.Text = brand.Description;
                imgLogo.ImageUrl = GetImagePath(brand);
                imgLogo.AlternateText = brand.Name;
            }

        }

        protected void HandlePage()
        {
            if (Locator.GetService<IContextObjects>().Get<CustomerHolder>() == null)
                AccessDeniedMessage();
            else
            {
                Customer cc = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
                switch (cc.Role)
                {
                    case CustomerRole.Buyer:
                        AccessDeniedMessage();
                        break;
                    case CustomerRole.Manager:
                        btnDelete.Visible = false;
                        break;
                    case CustomerRole.Admin:
                        break;
                    default:
                        break;

                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Brand brand = new Brand();
            if (id > 0)
                brand = Locator.GetService<IBrandManager>().Load(id);
            brand.Name = tbName.Text;
            brand.Description = tbDescription.Text;
            Locator.GetService<IBrandManager>().Save(brand);
            if (fuImage.PostedFile != null)
            {
                FileUtils.UploadImg(fuImage.PostedFile, HttpContext.Current.Server.MapPath("images") + "\\" + "Brand/", string.Format("IMG{0}.jpg", brand.Id),HttpContext.Current.Server.MapPath("images") + "\\" + "Default\\watermark.gif");
            }
            Response.Redirect("BrandDetails.aspx?action=view&id="+brand.Id.ToString());
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("BrandDetails.aspx?action=edit&id=" + id.ToString());
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("BrandList.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Brand brand = null;
            if (id > 0)
                brand = Locator.GetService<IBrandManager>().Load(id);
            if (brand != null)
            {
                Locator.GetService<IBrandManager>().Delete(brand);
            }
            Response.Redirect("BrandList.aspx");
        }
    }
}
